//
//  UIViewController+MBProgressHUD.swift
//  ClassPlatform
//
//  Created by zhulinpeng on 17/1/3.
//  Copyright © 2017年 ABC360. All rights reserved.
//

import UIKit
import MBProgressHUD

extension UIViewController {

    private struct AssociatedKeys {
        static var HUDName = "HUDName"
        static var HUDWating = "HUDWating"
    }
    
    var progressHUD: MBProgressHUD? {
        get {
            if let hud = objc_getAssociatedObject(self, &AssociatedKeys.HUDName) as? MBProgressHUD {
                return hud
            } else {
                let hud = MBProgressHUD(view: view)
                hud.removeFromSuperViewOnHide = true
                view.addSubview(hud)
                
                self.progressHUD = hud
                
                return hud
            }
        }
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.HUDName, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    var jwHUDIsWating: Bool? {
        get {
            if let hud = objc_getAssociatedObject(self, &AssociatedKeys.HUDWating) as? Bool {
                return hud
            } else {
                let hud = false
                return hud
            }
        }
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.HUDWating, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
}

extension UIViewController {
    
    typealias HUDCompletion = (() -> ())
    
    /// 转菊花
   @objc func showHUD() {

        self.showHUD(duration: -1) { }
    }
    
    /// 延迟转菊花
    func delayShowHUD() {

        //延时1秒执行
        self.jwHUDIsWating = false
        let time: TimeInterval = 0.5
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + time) {
            //code
            if self.jwHUDIsWating!  {
                return
            }
            self.showHUD(duration: -1) { }
        }
    }

    @objc func showHUD(duration: TimeInterval) {

        showHUD(duration: duration) { }
    }
    
    // duration < 0 表示不自动隐藏 HUD
    @objc func showHUD(duration: TimeInterval, completion: @escaping HUDCompletion) {

        if progressHUD?.superview == nil {
            progressHUD = nil   // 设置成nil后重新初始化
        }
        
        progressHUD?.mode = MBProgressHUDMode.indeterminate
        progressHUD?.completionBlock = completion
        progressHUD?.contentColor    = UIColor.white
        progressHUD?.bezelView.color = UIColor.black
        progressHUD?.bezelView.blurEffectStyle = .dark
        progressHUD?.show(animated: true)
        if duration >= 0 {
            progressHUD?.hide(animated: true, afterDelay: duration)
        }
    }
    
    /// 转菊花附带文字
    @objc func showHUD(withDetail detail: String) {

        showHUD(withDetail: detail, duration: -1) { }
    }
    
    @objc func showHUD(withDetail detail: String, duration: TimeInterval) {

        showHUD(withDetail: detail, duration: duration) { }
    }
    
    @objc func showHUD(withDetail detail: String, duration: TimeInterval, completion: @escaping HUDCompletion) {

        if progressHUD?.superview == nil {
            progressHUD = nil
        }
        
        progressHUD?.mode = MBProgressHUDMode.indeterminate
        progressHUD?.completionBlock = completion
        progressHUD?.contentColor    = UIColor.white
        progressHUD?.bezelView.color = UIColor.black
        progressHUD?.bezelView.blurEffectStyle = .dark
        progressHUD?.label.text = detail
        progressHUD?.show(animated: true)
        if duration > 0 {
            progressHUD?.hide(animated: true, afterDelay: duration)
        }
    }
    
    /// 只带文字的 HUD
    @objc func showHUDTextOnly(_ text: String, duration: TimeInterval = 1.5) {

        showHUDTextOnly(text, duration: duration) { }
    }
    
    @objc func showHUDTextOnly(_ text: String, duration: TimeInterval = 1.5, completion: @escaping HUDCompletion) {

        DispatchQueue.main.async {
            self.showHUDTextOnly_main(text, duration: duration, completion: completion)
        }
    }
    
    
    func showHUDTextOnly_main(_ text: String, duration: TimeInterval = 1.5, completion: @escaping HUDCompletion) {


        if progressHUD?.superview == nil {
            progressHUD = nil
        }
        
        progressHUD?.mode = MBProgressHUDMode.text
        progressHUD?.margin = 10.0
        progressHUD?.label.text = text
        progressHUD?.label.numberOfLines = 0
        progressHUD?.completionBlock = completion
        progressHUD?.contentColor    = UIColor.white
        progressHUD?.bezelView.color = UIColor.black
        progressHUD?.bezelView.blurEffectStyle = .dark
        progressHUD?.show(animated: true)
        progressHUD?.hide(animated: true, afterDelay: duration)
    }
    
    /// 圆环进度条
    func showAnnularProgressHUD(withDetail detail: String?) {

        if progressHUD?.superview == nil {
            progressHUD = nil
        }
        
        progressHUD?.mode = MBProgressHUDMode.annularDeterminate
        if detail != nil {
            progressHUD?.label.text = detail
        }
        progressHUD?.show(animated: true)
    }
    
    func setAnnularHUDProgress(progress: Float) {

        if progressHUD?.mode != MBProgressHUDMode.annularDeterminate {
            return
        }
        progressHUD?.progress = progress
    }
    
    /// 隐藏 HUD
    @objc func dismissHUD() {

        self.jwHUDIsWating = true
        DispatchQueue.main.async {
            self.progressHUD?.hide(animated: true)
            self.progressHUD = nil
        }
    }
    
    class func initialize(from storyboardName: String, identifier: String, bundle: Bundle? = nil) -> UIViewController {

        return UIStoryboard(name: storyboardName, bundle: bundle).instantiateViewController(withIdentifier: identifier)
    }
}
